var app = getApp()
var auth = require('../../lib/auth/login')
var tools = require('../../lib/system/tools')
var pages = require('../../lib/common/pageConstant')
var commonFunc = require('../../lib/system/common')

Page({
    data: {
        mobile: '',
        captcha: '',
        mobileFocus: false,
        resourcePage: '',
        isCaptchaBtnDisabled: false,
        captchaCountDownTime: 60,
        captchaBtnText: '获取验证码',
        captchaCountDownTimeFlag: null
    },
    onLoad: function (query) {
        var resourcePage = commonFunc.getParam(query, 'resource_page', '')
        this.setData({
            resourcePage: resourcePage
        })
    },
    mobileInput: function (event) {
        var currentMobile = event.detail.value
        var mobileReg = /^1[34578]\d{9}$/g
        if (!mobileReg.test(currentMobile)) {
            this.setData({
                mobileFocus: true
            })
            console.log('手机号验证失败')
            return false
        }
        this.setData({
            mobile: currentMobile
        })
        return true
    },
    captchaInput: function (event) {
        this.setData({
            captcha: event.detail.value
        })
    },
    checkMobile: function (mobile) {
        if (!mobile) {
            wx.showToast({
                title: '手机号未输入',
                icon: 'success',
                duration: 1000
            })
            return false
        }
        return true;
    },
    opeCaptchaCountDown: function (ope) {
        if (ope == 'boot') {
            var countDownTime = Math.max(this.data.captchaCountDownTime - 1, 0)
            this.setData({
                captchaCountDownTime: countDownTime,
                captchaBtnText: '重试(' + countDownTime + 's)'
            })
        } else {
            this.setData({
                isCaptchaBtnDisabled: false,
                captchaCountDownTime: 60,
                captchaBtnText: '获取验证码'
            })
            clearInterval(this.data.capdcaptchaCountDownTimeFlag)
        }
    },
    pauseCaptchaBtn: function (event) {
        var currentCountDownTime = this.data.captchaCountDownTime
        if (this.data.isCaptchaBtnDisabled && currentCountDownTime) {
            this.opeCaptchaCountDown('boot') 
        } else {
            if (currentCountDownTime <= 0) {
                this.opeCaptchaCountDown('over') 
                return false
            }
            if (this.data.capdcaptchaCountDownTimeFlag) {
                clearInterval(this.data.capdcaptchaCountDownTimeFlag)
            }
            this.setData({
                isCaptchaBtnDisabled: true,
                capdcaptchaCountDownTimeFlag: setInterval(this.pauseCaptchaBtn, 1000)
            })
        }
        return true
    },
    sendCaptcha: function (event) {
        if (!this.checkMobile(this.data.mobile)) {
            return false
        }
        tools.requestWithAuth({
            url: 'send_verify_code',
            data: {
                phone_no: this.data.mobile
            },
            feedback: {
                success: event => {
                    this.pauseCaptchaBtn()
                    wx.showToast({
                        title: '验证码发送成功',
                        icon: 'success',
                        duration: 2000
                    })
                }
            }
        })
    },
    submitMobile: function (event) {
        if (!this.checkMobile(this.data.mobile)) {
            return false
        }
        if (!this.data.captcha) {
            wx.showToast({
                title: '验证码未输入',
                icon: 'success',
                duration: 2000
            })
            return false
        }
        var that = this
        tools.requestWithAuth({
            url: 'bind_phone_no',
            data: {
                phone_no: that.data.mobile,
                verify_code: that.data.captcha
            },
            feedback: {
                success: event => {
                    auth.bindUser(that.data.mobile, function (){
                        console.log('user bind success')
                        wx.showToast({
                            title: '手机号绑定成功',
                            icon: 'success',
                            duration: 2000,
                            success: function (e) {
                                console.log('bind success tips over')
                                console.log('page:' + that.resourcePage)
                                if (pages.tabBar.indexOf(that.resourcePage) >= 0) {
                                    wx.switchTab({
                                        url: resourcePage
                                    })
                                } else {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                }
                            }
                        })
                    })
                }
            }
        })
        console.log('mobile:' + this.data.mobile)
    }
})